package com.samulegao.android.pis;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.samulegao.android.pis.ToDos.ToDoColumn;

public class MainActivity extends Activity {

	private static final String TAG = "MainActivity";

	public static final int MENU_ITEM_DELETE = Menu.FIRST;
	public static final int MENU_ITEM_DELETE_ALL = Menu.FIRST + 1;
	public static final int MENU_ITEM_SHOW_ALL = Menu.FIRST + 2;
	public static final int MENU_ITEM_EXIT = Menu.FIRST + 3;

	boolean showAll = false;

	EditText newTODOsEditText;
	Button newTODOsButton;
	ListView allTODOsListView;

	private static final String[] PROJECTION = new String[] { ToDoColumn._ID,
			ToDoColumn.TITLE, ToDoColumn.DEADLINE_TIME, ToDoColumn.FINISH };
	private static final int TITLE_INDEX = 1;
	// private static final int DEADLINE_TIME_INDEX = 2;
	// private static final int FINISH_INDEX = 3;
	private Cursor mCursor = null;
	private NotSimpleCursorAdapter adapter = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		newTODOsEditText = (EditText) findViewById(R.id.edit_text_new_todos);

		newTODOsButton = (Button) findViewById(R.id.button_new_todos);
		newTODOsButton.setOnClickListener(newTODOsButton_OnClickListener);

		allTODOsListView = (ListView) findViewById(R.id.list_view_todos);

		mCursor = managedQuery(ToDoColumn.CONTENT_URI, PROJECTION,
				ToDoColumn.FINISH + "=0", null, null);
		adapter = new NotSimpleCursorAdapter(this,
				R.layout.two_line_list_item_checked, mCursor, true);

		allTODOsListView.setAdapter(adapter);

		allTODOsListView.setOnItemClickListener(onListItemClickListener);
		allTODOsListView
				.setOnCreateContextMenuListener(onCreateContextMenuListener);

		ActivityManager acitivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningServiceInfo> services = acitivityManager
				.getRunningServices(100);
		Log.e("service", "" + services.size());
		for (RunningServiceInfo s : services) {
			Log.e("service", s.process + ":" + s.service.flattenToShortString());
		}
	}

	private OnClickListener newTODOsButton_OnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setAction(Intent.ACTION_INSERT);
			intent.setData(ToDos.ToDoColumn.CONTENT_URI);
			intent.putExtra(ToDoColumn.TITLE, newTODOsEditText.getText()
					.toString());
			startActivity(intent);
		}
	};

	private OnItemClickListener onListItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Uri uri = ContentUris.withAppendedId(ToDoColumn.CONTENT_URI, id);
			startActivity(new Intent(Intent.ACTION_EDIT, uri));
		}
	};

	private OnCreateContextMenuListener onCreateContextMenuListener = new OnCreateContextMenuListener() {

		@Override
		public void onCreateContextMenu(ContextMenu menu, View v,
				ContextMenuInfo menuInfo) {
			AdapterView.AdapterContextMenuInfo info = null;

			try {
				info = (AdapterView.AdapterContextMenuInfo) menuInfo;
			} catch (Exception e) {
				Log.v(TAG, "bad menuInfo", e);
				return;
			}

			Cursor cursor = (Cursor) allTODOsListView.getAdapter().getItem(
					info.position);
			if (cursor == null) {
				return;
			}

			menu.setHeaderTitle(cursor.getString(TITLE_INDEX));
			menu.add(0, MENU_ITEM_DELETE, Menu.NONE, R.string.menu_delete);
			menu.add(0, MENU_ITEM_DELETE_ALL, Menu.NONE,
					R.string.menu_delete_all);
		}
	};

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info;
		try {
			info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		} catch (ClassCastException e) {
			Log.e(TAG, "bad menuInfo", e);
			return false;
		}

		switch (item.getItemId()) {
		case MENU_ITEM_DELETE: {
			Uri todoUri = ContentUris.withAppendedId(ToDoColumn.CONTENT_URI,
					info.id);
			getContentResolver().delete(todoUri, null, null);
			return true;
		}
		case MENU_ITEM_DELETE_ALL: {
			getContentResolver().delete(ToDoColumn.CONTENT_URI, null, null);
			return true;
		}
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_ITEM_SHOW_ALL, Menu.NONE, R.string.menu_show_all)
				.setIcon(android.R.drawable.ic_menu_view);
		menu.add(0, MENU_ITEM_EXIT, Menu.NONE, R.string.menu_exit).setIcon(
				android.R.drawable.ic_menu_close_clear_cancel);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_ITEM_SHOW_ALL:
			if (!showAll) {
				item.setTitle(R.string.menu_hide_all);
				mCursor = managedQuery(ToDoColumn.CONTENT_URI, PROJECTION,
						null, null, null);
			} else {
				item.setTitle(R.string.menu_show_all);
				mCursor = managedQuery(ToDoColumn.CONTENT_URI, PROJECTION,
						ToDoColumn.FINISH + "=0", null, null);
			}
			showAll = !showAll;
			adapter.changeCursor(mCursor);
			break;
		case MENU_ITEM_EXIT:
			this.finish();
			break;
		}
		return true;
	}

	@Override
	protected void onResume() {
		newTODOsEditText.setText("");
		super.onResume();
	}
}